package com.market.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.market.auto.JSONAuthentication;
import com.market.entity.MaInfomation;
import com.market.entity.MaUser;
import com.market.mapper.MaInfomationMapper;
import com.market.response.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.crypto.Data;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author market
 * @since 2022-02-28
 */
@RestController
@RequestMapping("/ma-infomation")
public class MaInfomationController extends JSONAuthentication {
    @Autowired
    MaInfomationMapper maInfomationMapper;
    @ApiOperation(value = "添加公告")
    @PostMapping("/add")
    public void addInfomation(HttpServletRequest request, HttpServletResponse response, @RequestParam("title") String title,@RequestParam("content") String content) throws Exception{
        MaInfomation maInfomation=new MaInfomation();
        maInfomation.setTitle(title);
        maInfomation.setContent(content);
        Date date=new Date();
        Timestamp timestamp=new Timestamp(date.getTime());
        maInfomation.setCreatTime(timestamp);
        if(maInfomationMapper.insert(maInfomation)>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        }else{
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }

    @ApiOperation(value = "删除公告")
    @PostMapping("/del")
    public void delInfomation(HttpServletRequest request, HttpServletResponse response, @RequestParam("userId") int userId ) throws Exception{
        int i=maInfomationMapper.deleteById(userId);
        if(i>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "修改公告标题")
    @PostMapping("/updInfomationTitle")
    public void updInfomationTitle(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int  id,@RequestParam("title") String title)throws Exception{
        MaInfomation maInfomation = maInfomationMapper.selectById(id);
        maInfomation.setTitle(title);
        Date date=new Date();
        Timestamp timestamp=new Timestamp(date.getTime());
        maInfomation.setUpdTime(timestamp);
        if(maInfomationMapper.updateById(maInfomation)>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "修改公告内容")
    @PostMapping("/updInfomationContent")
    public void updInfomationContent(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int  id,@RequestParam("content") String content)throws Exception{
        MaInfomation maInfomation = maInfomationMapper.selectById(id);
        maInfomation.setContent(content);
        Date date=new Date();
        Timestamp timestamp=new Timestamp(date.getTime());
        maInfomation.setUpdTime(timestamp);
        if(maInfomationMapper.updateById(maInfomation)>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "通过公告标题查询公告")
    @PostMapping("/lookInfomationByTitle")
    public void lookInfomationByTitle(HttpServletRequest request, HttpServletResponse response,@RequestParam("title") String  title,@RequestParam("size") int size,@RequestParam ("current") int current)throws  Exception{

        QueryWrapper wrapper=new QueryWrapper();
        wrapper.like("title",title);
        Page<MaInfomation> shUserPage=new Page<>(current,size);
        Page<MaInfomation> shUserPages=maInfomationMapper.selectPage(shUserPage,wrapper);
        List<MaInfomation> records = shUserPages.getRecords();
        int total=maInfomationMapper.selectCount(wrapper);
        if(records.size()>0){
            Result ok = Result.ok().data("userList",records).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查找所有公告")
    @PostMapping("/lookInfomation")
    public void lookInfomation(HttpServletRequest request, HttpServletResponse response,@RequestParam("size") int size,@RequestParam ("current") int current)throws  Exception{
       int total=maInfomationMapper.selectCount(null);
        Page<MaInfomation> shUserPage=new Page<>(current,size);
        Page<MaInfomation> shUserPages=maInfomationMapper.selectPage(shUserPage,null);
        List<MaInfomation> records = shUserPages.getRecords();
        if(records.size()>0){
            Result ok = Result.ok().data("userList",records).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }

}

